/*
 * Landmark service object based on com.nokia.device.landmarks  Interface
 */
var landmark = null;

/*
 * Initializes landmark service object
 */
function initService(){
    try {
        landmark = nokia.device.load("landmarks");
    } 
    catch (e) {
        alert(e.toString());
    }
}

/*
 * Gets landmarks of specified type
 * type - type of landmarks to be listed
 */
function listLandmarks(type){
    try {
        var filter = new Object();
        if (type != null && type != "") 
            filter.categoryId = type;
        var transactionID = landmark.getLandmarks(onDataAvailable, filter, onDataAvailableErr);
    } 
    catch (e) {
        alert(e.toString());
    }
}

/*
 * Gets a list of categories
 */
function populateCategories(){
    try {
        landmark.getCategories(onCategoriesAvailable, null, onCategoriesAvailableErr);
    } 
    catch (e) {
        var error = e.toString();
        alert(error);
    }
}

/*
 * Starts the device native landmark editor
 */
function editLandmarks(){
    try {
        landmark.startEditor(onStart);
    } 
    catch (e) {
        var error = e.toString();
        alert(error);
    }
}

/*
 * Callback handler for startEditor
 */
function onStart(){
	//Do something
}

/*
 * Callback handler for getCategories
 * populates filterType select object with categories options
 */
function onCategoriesAvailable(outList){
    var outputEntry = null;
    var typeSelect = document.getElementById("filterType");
    var i = 1;
    while ((outputEntry = outList.next()) != null) {
        typeSelect.options[i] = new Option(outputEntry.name, outputEntry.name);
        i++;
    }
}

/*
 * Error Callback handler for getCategories
 */
function onCategoriesAvailableErr(err){
    alert(err.toString());
}

/*
 * Callback handler for getLandmarks
 * Populates landmarks container with retrieved landmarks data
 */
function onDataAvailable(outList){
    var txt = "<table border='0'><tr><th><input type='checkbox' name='chAll' onClick='chALL(this.checked)'/></th>" +
    "<th colspan='3'><ul class='nav-horizontal-rounded stack'><li class='first two-piece'><a href='#' onclick='showSelected();'>Show selected</a></li>" +
    "<li class='last two-piece'><a href='#' onclick='editLandmarks();'>Edit landmarks</a></li></ul></th></tr>";
    
        var outputEntry = null;
        while (outList.hasNext()) {
            outputEntry = outList.next();
            marks[outputEntry.id] = outputEntry;
            txt += "<tr><td width='5'><input type='checkbox' onclick='checkMain()' name='ch' value='" + outputEntry.id + "'/></td><td><a href=\"#\" onClick=\"showLandmark(" + outputEntry.id + ", true)\">" + outputEntry.name + "</a></td>";
            txt += "<td>";
			if (outputEntry.address != null) {
				if (outputEntry.address.street != null && outputEntry.address.street != "") 
					txt += outputEntry.address.street;
				if (outputEntry.address.locale != null && outputEntry.address.locale != "") 
					txt += ", " + outputEntry.address.locale;
				if (outputEntry.address.region != null && outputEntry.address.region != "") 
					txt += ", " + outputEntry.address.region;
				if (outputEntry.address.code != null && outputEntry.address.code != "") 
					txt += ", " + outputEntry.address.code;
				if (outputEntry.address.counry != null && outputEntry.address.country != "") 
					txt += ", " + outputEntry.address.country;
			}else
			    {
            	txt += "no address";
            }
            txt += "</td></tr>";
        }
    txt += "</table>";
    document.getElementById("landmarksContainer").innerHTML = txt;
}

/*
 * Error Callback handler for getLandmarks
 */
function onDataAvailableErr(err){
    alert(err.toString());
}
